package com.android.mymedicine.infrastructure.impl;

import com.android.mymedicine.domain.user.User;
import com.android.mymedicine.domain.user.UserRepository;
import com.android.mymedicine.infrastructure.SQLiteHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;

public class UserRepositoryImpl implements UserRepository{

	Dao<User, Integer> userDao;

	public UserRepositoryImpl(SQLiteHelper db) {
		super();
		try {
			userDao = db.getDao(User.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void create(User user){
		try {
			userDao.create(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean doLogin(User user) {
		try {
			QueryBuilder<User, Integer> qb = userDao.queryBuilder();
			qb.where().eq("userName", user.getUserName());
			qb.where().eq("password", user.getPassword());
			
			PreparedQuery<User> pq = qb.prepare();
			if(userDao.queryForFirst(pq) !=null){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public void doLogout(User user) {

	}

	
}
